package com.michaelbulava.TFS.ui.controls.idea;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.intellij.util.EventDispatcher;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.ui.controls.generic.IProjectDependantControl;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mbulava on 7/14/2015.
 */
public class SelectProjectOrModuleControl extends JPanel implements IProjectDependantControl {
    private JTextPane wouldYouLikeToTextPane;
    private JRadioButton entireProjectRadioButton;
    private JRadioButton selectedModuleSRadioButton;
    private ModuleCheckboxList moduleCheckboxList;
    private final EventDispatcher<ChangeListener> myEventDispatcher = EventDispatcher.create(ChangeListener.class);

    private boolean checkedAllModuleSubPaths = false;
    public void addChangeListener(ChangeListener value){
        myEventDispatcher.addListener(value);
    }

    public SelectProjectOrModuleControl(){
        super();
        setLayout(new GridLayoutManager(4, 2, new Insets(4, 4, 4, 4), -1, -1));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        add(panel2, new GridConstraints(0, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                null, null, null, 0, false));

        wouldYouLikeToTextPane = new JTextPane();
        wouldYouLikeToTextPane.setText(Messages.getString("SelectProjectOrModuleControl.HeadingText"));
        panel2.add(wouldYouLikeToTextPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW,
                null, new Dimension(150, 50), null, 0, false));

        entireProjectRadioButton = new JRadioButton();
        entireProjectRadioButton.setText(Messages.getString("SelectProjectOrModuleControl.EntireProjectRadioButton"));
        add(entireProjectRadioButton, new GridConstraints(1, 0, 1, 2, GridConstraints.ANCHOR_WEST,
                GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        entireProjectRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (!checkedAllModuleSubPaths && entireProjectRadioButton.isSelected()) {
                    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    Module[] modules = ModuleManager.getInstance(getProject()).getModules();
                    boolean failed = false;
                    for (int i = 0; i < modules.length; i++) {
                        if (!VfsUtil.isAncestor(getProject().getBaseDir(), modules[i].getModuleFile(), false)) {
                            Messages.showInfoMessage(SelectProjectOrModuleControl.this, "message", "title");
                            failed = true;
                            break;
                        }
                    }
                    setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                    if (failed)
                        return;
                    else
                        checkedAllModuleSubPaths = true;
                }
                selectedModuleSRadioButton.setSelected(!entireProjectRadioButton.isSelected());
                moduleCheckboxList.setEnabled(!entireProjectRadioButton.isSelected());
                myEventDispatcher.getMulticaster().stateChanged(new ChangeEvent(SelectProjectOrModuleControl.this));
            }
        });

        selectedModuleSRadioButton = new JRadioButton();
        selectedModuleSRadioButton.setText(Messages.getString("SelectProjectOrModuleControl.SelectedModuleSRadioButton"));
        selectedModuleSRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                entireProjectRadioButton.setSelected(!selectedModuleSRadioButton.isSelected());
                moduleCheckboxList.setEnabled(selectedModuleSRadioButton.isSelected());
                myEventDispatcher.getMulticaster().stateChanged(new ChangeEvent(SelectProjectOrModuleControl.this));
            }

        });
        add(selectedModuleSRadioButton, new GridConstraints(2, 0, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));

        moduleCheckboxList = new ModuleCheckboxList();
        moduleCheckboxList.setEnabled(false);
        moduleCheckboxList.setCheckItemsStateChangedListener(new ModuleCheckboxList.CheckItemsStateChangedListener() {
            @Override
            public void onCheckItemsStateChanged() {
                myEventDispatcher.getMulticaster().stateChanged(new ChangeEvent(SelectProjectOrModuleControl.this));
            }
        });

        add(moduleCheckboxList, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));

        final Spacer spacer1 = new Spacer();
        add(spacer1, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1,
                GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
    }

    public void setProject(Project project){
        moduleCheckboxList.setProject(project);
    }

    @Override
    public Project getProject() {
        return moduleCheckboxList.getProject();
    }

    public void setSelectedModule(Module module){
        if (moduleCheckboxList.getAllListItems().contains(module)){
            selectedModuleSRadioButton.setSelected(true);
            moduleCheckboxList.setCheckedListItems(new Module[] { module });
        }
    }

    public void setSelectedModules(Module[] modules){
        List<Module> selected = new ArrayList<Module>();
        for(int i = 0; i < modules.length; i++){
            if (moduleCheckboxList.getAllListItems().contains(modules[i])){
                selectedModuleSRadioButton.setSelected(true);
                selected.add(modules[i]);
            }
        }
        moduleCheckboxList.setCheckedListItems(selected.toArray(new Module[0]));
    }

    public boolean isEntireProject(){
        return entireProjectRadioButton.isSelected();
    }

    public Module[] getSelectedModules(){
        return moduleCheckboxList.getCheckedListItems().toArray(new Module[0]);
    }

    public void setEntireProject(boolean value){
        entireProjectRadioButton.setSelected(value);
    }
}
